#include <stdio.h>
#include "oled.h"
#include "font.h"

spi_device_handle_t MY_OLED_Handle;

// uint8_t OLED_GRAM[8][128];
uint8_t oledBuffer[OLED_SIZE_BUFFER];
/////////////////////////////////////// 32*32 图标数据//////////////////////////////////////
const uint8_t data_dis_1[] =
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x78, 0x40,
        0x40, 0x78, 0x40, 0x40, 0x40, 0x78, 0x40, 0x40, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x48, 0x48, 0x48, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x48, 0x48, 0x48, 0x00, 0x00,
        0x00, 0x00, 0x12, 0x12, 0x12, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x20,
        0x20, 0x20, 0x20, 0x20, 0x20, 0x3F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x12, 0x12, 0x12, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x04, 0x04, 0x04, 0x3C, 0x04, 0x04, 0x3C, 0x04,
        0x04, 0x3C, 0x04, 0x04, 0x3C, 0x04, 0x04, 0x04, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\?????????.bmp",0*/
};

const uint8_t data_dis_2[] =
    {
        0x00, 0x00, 0xF0, 0xFC, 0xFC, 0xFE, 0x0F, 0x07, 0x00, 0x00, 0x00, 0xE0, 0xFC, 0xFE, 0xFE, 0x0F,
        0x07, 0x00, 0x00, 0x00, 0xE0, 0xFC, 0xFE, 0xFE, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x03, 0x07, 0x0F, 0x1F, 0x3E, 0xF8, 0xF0, 0xE0, 0x80, 0x03, 0x07, 0x0F, 0x1F,
        0x3E, 0xF8, 0xF0, 0xE0, 0x81, 0x03, 0x07, 0x0F, 0x1F, 0x3E, 0xF8, 0xF0, 0xE0, 0x80, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x30, 0x3C, 0x3F, 0x1F, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x30,
        0x3C, 0x3F, 0x1F, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x38, 0x3C, 0x3F, 0x1F, 0x0F, 0x03, 0x00, 0x00,
        0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE6, 0xE6, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7,
        0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, /*"D:\xmfu_github\??????\UI???\????.bmp",0*/
};

const uint8_t data_dis_3[] =
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xF0, 0x18, 0x1C, 0x0C, 0x0C, 0x08, 0x10, 0x10, 0x10,
        0x10, 0x10, 0x10, 0x08, 0x0C, 0x0C, 0x1C, 0x18, 0xF0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0xE0, 0x30, 0x10, 0x1C, 0x07, 0x03, 0x00, 0x00, 0x80, 0xE0, 0x30, 0x18, 0x08, 0x0C,
        0x0C, 0x08, 0x18, 0x30, 0xE0, 0x80, 0x00, 0x00, 0x03, 0x07, 0x1C, 0x10, 0x30, 0xE0, 0x00, 0x00,
        0x00, 0x00, 0x0F, 0x0C, 0x18, 0x38, 0xE0, 0xC0, 0x00, 0x00, 0x01, 0x07, 0x0C, 0x18, 0x10, 0x30,
        0x30, 0x10, 0x18, 0x0C, 0x07, 0x01, 0x00, 0x00, 0xC0, 0xE0, 0x38, 0x18, 0x0C, 0x0F, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, 0x18, 0x18, 0x30, 0x30, 0x30, 0x18, 0x08, 0x08,
        0x08, 0x08, 0x18, 0x30, 0x30, 0x30, 0x18, 0x18, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\????.bmp",0*/

};

const uint8_t data_dis_4[] =
    {
        0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0C, 0x0C, 0x0C, 0x0C, 0x8C, 0x8C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1C, 0xF8, 0xF0, 0x00, 0x00,
        0x00, 0x00, 0xFF, 0xFF, 0x80, 0x80, 0xE0, 0xF8, 0x1E, 0x07, 0x1F, 0xFE, 0xE0, 0x00, 0x00, 0x00,
        0xE0, 0xF8, 0xF8, 0xE0, 0x80, 0x00, 0x80, 0xE0, 0xE0, 0xC0, 0x80, 0x80, 0xFF, 0xFF, 0x00, 0x00,
        0x00, 0x00, 0xFF, 0xFF, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0F, 0x7F, 0x7C, 0x1F,
        0x03, 0x00, 0x00, 0x03, 0x0F, 0x1E, 0x1F, 0x07, 0x00, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0x00, 0x00,
        0x00, 0x00, 0x0F, 0x1F, 0x38, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
        0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x38, 0x1F, 0x0F, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\????.bmp",0*/
};

const uint8_t data_dis_5[] =
    {
        0x00, 0x00, 0x80, 0xC0, 0x20, 0x10, 0x08, 0x0C, 0x04, 0x02, 0x02, 0x02, 0x01, 0x01, 0x81, 0x81,
        0x81, 0x81, 0x01, 0x01, 0x02, 0x02, 0x02, 0x04, 0x0C, 0x08, 0x10, 0x20, 0xC0, 0x80, 0x00, 0x00,
        0xF0, 0x0E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xE0,
        0xE0, 0x01, 0xFE, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0E, 0xF0,
        0x0F, 0x70, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xC6, 0x9B, 0x7C, 0x7F,
        0x7F, 0x3C, 0x93, 0x6C, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x70, 0x0F,
        0x00, 0x00, 0x01, 0x03, 0x04, 0x08, 0x10, 0x30, 0x20, 0x40, 0x40, 0x40, 0x80, 0x80, 0x81, 0x81,
        0x81, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x20, 0x30, 0x10, 0x08, 0x04, 0x03, 0x01, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\???????.bmp",0*/
};

const uint8_t data_dis_6[] =
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x7C, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F,
        0x3F, 0x7E, 0xFC, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8,
        0xFC, 0x7E, 0x3F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x1C, 0x3E, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\??.bmp",0*/

};

const uint8_t data_dis_7[] =
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0x7C, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFC, 0x7E, 0x3F,
        0x1F, 0x0F, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x3F, 0x7E, 0xFC,
        0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x3E, 0x1C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"D:\xmfu_github\??????\UI???\??.bmp",0*/
};

//////////////////////////////////////////////////////////////////////

void oled_write_Byte(uint8_t data, uint8_t cmd)
{
    if (cmd == OLED_DATA)
    {
        OLED_DC(1);
    }
    else
    {
        OLED_DC(0);
    }
    spi2_write_data(MY_OLED_Handle, &data, 1);
}

void oled_init(void)
{
    esp_err_t ret = 0;
    printf("OLED init start!\n");
    gpio_config_t gpio_init_struct;

    /* SPI驱动接口配置 */
    spi_device_interface_config_t devcfg = {
        .clock_speed_hz = 1 * 1000 * 1000, /* SPI时钟 */
        .mode = 0,                         /* SPI模式0 */
        .spics_io_num = -1,                /* SPI设备引脚 */
        .queue_size = 7,                   /* 事务队列尺寸 7个 */
    };

    /* 添加SPI总线设备 */
    ret = spi_bus_add_device(SPI2_HOST, &devcfg, &MY_OLED_Handle); /* 配置SPI总线设备 */
    ESP_ERROR_CHECK(ret);

    gpio_init_struct.intr_type = GPIO_INTR_DISABLE;        /* 失能引脚中断 */
    gpio_init_struct.mode = GPIO_MODE_OUTPUT;              /* 配置输出模式 */
    gpio_init_struct.pin_bit_mask = 1ull << OLED_RES_PIN;  /* 配置引脚位掩码 */
    gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE; /* 失能下拉 */
    gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;      /* 使能下拉 */
    gpio_config(&gpio_init_struct);                        /* 引脚配置 */

    printf("spi bus add OK!\n");

    // gpio_config_t gpio_init_struct = {0};

    gpio_init_struct.intr_type = GPIO_INTR_DISABLE;        /* 失能引脚中断 */
    gpio_init_struct.mode = GPIO_MODE_INPUT_OUTPUT;        /* 输入输出模式 */
    gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;      /* 使能上拉 */
    gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE; /* 失能下拉 */
    gpio_init_struct.pin_bit_mask = 1ull << OLED_DC_PIN;   /* 设置的引脚的位掩码 */
    gpio_config(&gpio_init_struct);                        /* 配置GPIO */

    OLED_DC(0);

    /*复位OLED*/
    OLED_RST(0);
    vTaskDelay(100);
    OLED_RST(1);
    vTaskDelay(100);

    /* 初始化代码 */
    oled_write_Byte(0xAE, OLED_CMD); /* 关闭显示 */
    oled_write_Byte(0xD5, OLED_CMD); /* 设置时钟分频因子,震荡频率 */
    oled_write_Byte(80, OLED_CMD);   /* [3:0],分频因子;[7:4],震荡频率 */
    oled_write_Byte(0xA8, OLED_CMD); /* 设置驱动路数 */
    oled_write_Byte(0X3F, OLED_CMD); /* 默认0X3F(1/64) */
    oled_write_Byte(0xD3, OLED_CMD); /* 设置显示偏移 */
    oled_write_Byte(0X00, OLED_CMD); /* 默认为0 */

    oled_write_Byte(0x40, OLED_CMD); /* 设置显示开始行 [5:0],行数 */
    oled_write_Byte(0x8D, OLED_CMD); /* 电荷泵设置 */
    oled_write_Byte(0x14, OLED_CMD); /* bit2，开启/关闭 */

    oled_write_Byte(0x20, OLED_CMD); /* 设置内存地址模式 */
    oled_write_Byte(0x10, OLED_CMD); /* [1:0],00，列地址模式;01，行地址模式;10,页地址模式;默认10; */

    oled_write_Byte(0xA1, OLED_CMD); /* 段重定义设置,bit0:0,0->0;1,0->127; */
    oled_write_Byte(0xC8, OLED_CMD); /* 设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数 */

    oled_write_Byte(0xDA, OLED_CMD); /* 设置COM硬件引脚配置 */
    oled_write_Byte(0x12, OLED_CMD); /* [5:4]配置 */

    oled_write_Byte(0x81, OLED_CMD); /* 对比度设置 */
    oled_write_Byte(0xCF, OLED_CMD); /* 1~255;默认0X7F (亮度设置,越大越亮) */

    oled_write_Byte(0xD9, OLED_CMD); /* 设置预充电周期 */
    oled_write_Byte(0xf1, OLED_CMD); /* [3:0],PHASE 1;[7:4],PHASE 2; */
    oled_write_Byte(0xDB, OLED_CMD); /* 设置VCOMH 电压倍率 */
    oled_write_Byte(0x40, OLED_CMD); /* [6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc; */

    oled_write_Byte(0xA4, OLED_CMD); /* 全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏) */
    oled_write_Byte(0xA6, OLED_CMD); /* 设置显示方式;bit0:1,反相显示;0,正常显示 */
    oled_write_Byte(0xAF, OLED_CMD); /* 开启显示 */

    /* 打开oled */
    oled_on();
    oled_clear();
}

/**
 * @brief       打开OLED
 * @param       无
 * @retval      无
 */
void oled_on(void)
{
    oled_write_Byte(0X8D, OLED_CMD);
    oled_write_Byte(0X14, OLED_CMD);
    oled_write_Byte(0XAF, OLED_CMD);
}

/**
 * @brief       关闭OLED
 * @param       无
 * @retval      无
 */
void oled_off(void)
{
    oled_write_Byte(0X8D, OLED_CMD);
    oled_write_Byte(0X10, OLED_CMD);
    oled_write_Byte(0XAE, OLED_CMD);
}

/**
 * @brief       清屏
 * @param       无
 * @retval      无
 */
void oled_clear(void)
{
    uint16_t i;
    for(i = 0; i < OLED_SIZE_BUFFER; i++)
    {
        oledBuffer[i] = 0;
    }
    // for (i = 0; i < 8; i++)
    // {
    //     for (n = 0; n < 128; n++)
    //     {
    //         OLED_GRAM[i][n] = 0X00;
    //     }
    // }
    oled_refresh_gram(); /* 更新显示 */
}

/**
 * @brief       OLED画点 注意取模方向
 * @param       x  : 0~127
 * @param       y  : 0~63
 * @param       dot: 1 填充 0,清空
 * @retval      无
 */
void oled_draw_point(uint8_t x, uint8_t y, uint8_t dot)
{
    uint8_t pos, bx, temp = 0;

    if (x > 127 || y > 63)
    {
        return; /* 超出范围了 */
    }

    pos = y / 8; /* 计算GRAM里面的y坐标所在的字节, 每个字节可以存储8个行坐标 */
    bx = y % 8;  /* 取余数,方便计算y在对应字节里面的位置,及行(y)位置 */
    // temp = 1 << (7 - bx);           /* 高位表示高行号, 得到y对应的bit位置,将该bit先置1 */  // 取模方向：顺向
    temp = 1 << (bx); /* 高位表示低行号, 得到y对应的bit位置,将该bit先置1 */ // 取模方向：逆向

    if (dot) /* 画实心点 */
    {
        // OLED_GRAM[pos][x] |= temp;
        oledBuffer[pos * 128 + x] |= temp;
    }
    else /* 画空点,即不显示 */
    {
        // OLED_GRAM[pos][x] &= ~temp;
        oledBuffer[pos * 128 + x] &= ~temp;
    }
}

/**
 * @brief       OLED填充区域填充
 * @note:       注意:需要确保: x1<=x2; y1<=y2  0<=x1<=127  0<=y1<=63
 * @param       x1,y1: 起点坐标
 * @param       x2,y2: 终点坐标
 * @param       dot: 1 填充 0,清空
 * @retval      无
 */
void oled_fill(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t dot)
{
    uint8_t x, y;

    for (x = x1; x <= x2; x++)
    {
        for (y = y1; y <= y2; y++)
        {
            oled_draw_point(x, y, dot);
        }
    }

    oled_refresh_gram(); /* 更新显示 */
}

/**
 * @brief       OLED显示16*8大小的字符串
 * @note:       注意:需要确保: 0<=x<=127  0<=y<=63
 * @param       x,y: 起点坐标
 * @param       chr: 字符
 * @retval      无
 */
void oled_show_char_ASCII(uint8_t x, uint8_t y, char chr, uint8_t size)
{
    uint8_t i;
    uint8_t id = 0;

    y = y / 8;

    if ((chr > '~') || (chr < ' '))
    {
        return;
    }
    else
    {
        for (i = 0; i < (size / 2); i++)
        {
            id = chr - ' ';
            if (size == 12)
            {
                if (y + 1 > 8 || x + i > 127)
                {
                    return;
                }
                
                // OLED_GRAM[y + 0][x + i] = asc2_1206[id][i];
                // OLED_GRAM[y + 1][x + i] = asc2_1206[id][6 + i];
                oledBuffer[y * 128 + x + i] = asc2_1206[id][i];
                oledBuffer[(y + 1) * 128 + x + i] = asc2_1206[id][6 + i];
                
            }
            else if (size == 16)
            {
                if (y + 1 > 8 || x + i > 127)
                {
                    return;
                }
                // OLED_GRAM[y + 0][x + i] = asc2_1608[id][i];
                // OLED_GRAM[y + 1][x + i] = asc2_1608[id][8 + i];
                oledBuffer[y * 128 + x + i] = asc2_1608[id][i];
                oledBuffer[(y + 1) * 128 + x + i] = asc2_1608[id][8 + i];
            }
            else if (size == 24)
            {
                if (y + 2 > 8 || x + i > 127)
                {
                    return;
                }
                // OLED_GRAM[y + 0][x + i] = asc2_2412[id][i];
                // OLED_GRAM[y + 1][x + i] = asc2_2412[id][12 + i];
                // OLED_GRAM[y + 2][x + i] = asc2_2412[id][24 + i];
                oledBuffer[y * 128 + x + i] = asc2_2412[id][i];
                oledBuffer[(y + 1) * 128 + x + i] = asc2_2412[id][12 + i];
                oledBuffer[(y + 2) * 128 + x + i] = asc2_2412[id][24 + i];
            }
            else if (size == 32)
            {
                if (y + 3 > 8 || x + i > 127)
                {
                    return;
                }
                // OLED_GRAM[y + 0][x + i] = asc2_3216[id][i];
                // OLED_GRAM[y + 1][x + i] = asc2_3216[id][16 + i];
                // OLED_GRAM[y + 2][x + i] = asc2_3216[id][32 + i];
                // OLED_GRAM[y + 3][x + i] = asc2_3216[id][48 + i];
                oledBuffer[y * 128 + x + i] = asc2_3216[id][i];
                oledBuffer[(y + 1) * 128 + x + i] = asc2_3216[id][16 + i];
                oledBuffer[(y + 2) * 128 + x + i] = asc2_3216[id][32 + i];
                oledBuffer[(y + 3) * 128 + x + i] = asc2_3216[id][48 + i];
            }
        }
    }
}

/**
 * @brief       显示字符串
 * @param       x,y         :起始坐标
 * @param       width,height:区域大小
 * @param       size        :选择字体 12/16/24/32
 * @param       p           :字符串首地址
 * @retval      无
 */
void dis_show_string(uint8_t x, uint8_t y, uint8_t width, uint8_t height, char *p, uint8_t size)
{
    uint8_t x0 = x;

    width += x;
    height += y;

    while ((*p <= '~') && (*p >= ' ')) /* 判断是不是非法字符! */
    {
        if (x >= width)
        {
            x = x0;
            y += size;
        }

        if (y >= height)
        {
            break; /* 退出 */
        }
        oled_show_char_ASCII(x, y, *p, size);
        x += size / 2;
        p++;
    }
}


uint8_t intToStr(int32_t num, char *str)
{
    uint8_t i = 0;
    uint8_t isNegative = 0;

    if (num < 0)
    {
        isNegative = 1;
        num = -num;
    }

    do
    {
        str[i++] = num % 10 + '0';
        num /= 10;
    } while (num > 0);

    if (isNegative)
    {
        str[i++] = '-';
    }

    str[i] = '\0';

    // 反转字符串
    uint8_t start = 0;
    uint8_t end = i - 1;
    while (start < end)
    {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
    return i;
}

/*
 * @brief       显示数字 高位0不显示
 * @param       x,y         :起始坐标
 * @param       width,height:区域大小
 * @param       size        :选择字体 12/16/24/32
 * @param       p           :字符串首地址
 * @retval      无
 */
void dis_show_num(uint8_t x, uint8_t y, int32_t num, uint8_t size)
{
    // uint8_t i = 0;
    char str[10];
    intToStr(num, str);
    dis_show_string(x, y, 128, 64, str, size);
    
}

void dis_show_num_dis_0(uint8_t x, uint8_t y, uint8_t leng, int32_t num, uint8_t size)
{
    char str[10];
    char str_b[10];
    intToStr(num, str);
    uint8_t len = strlen(str);

    for (uint8_t i = 0; i < leng - len; i++)
    {
        str_b[i] = '0';
    }
    dis_show_string(x, y, 128, 64, str_b, size);
    dis_show_string(x+((size/2)*(leng - len)), y, 128, 64, str, size);

}

/**
 * @brief       平方函数, m^n
 * @param       m: 底数
 * @param       n: 指数
 * @retval      无
 */
uint32_t oled_pow(uint8_t m, uint8_t n)
{
    uint32_t result = 1;

    while (n--)
    {
        result *= m;
    }

    return result;
}

/**
 * @brief 更新显示
 * 
 * 
 */
void oled_refresh_gram(void)
{
    oled_write_Byte(0xb0 + 0, OLED_CMD); /* 设置页地址（0~7） */
    oled_write_Byte(0x00, OLED_CMD);     /* 设置显示位置—列低地址 */
    oled_write_Byte(0x10, OLED_CMD);     /* 设置显示位置—列高地址 */
    OLED_DC(1);
    spi2_write_data(MY_OLED_Handle, &oledBuffer, OLED_SIZE_BUFFER);
}

/**
 * @brief   清除显示缓存
 *
 */
void oled_data_clear(void)
{
    uint16_t i;

    for (i = 0; i < OLED_SIZE_BUFFER; i++)
    {
        oledBuffer[i] = 0;
    }
}

void oled_show_dis_32_32_BMP(uint8_t bmp_x, uint8_t bmp_y, const uint8_t *data_dis)
{
    uint8_t i;
    bmp_y = bmp_y / 8;

    if (data_dis == NULL)
    {
        return;
    }

    for (i = 0; i < 32; i++)
    {
        if ((bmp_x + i < 128) && (bmp_y < 8))
            oledBuffer[bmp_y * 128 + bmp_x + i] = data_dis[i + 32 * 0];
        if ((bmp_x + i < 128) && (bmp_y + 1 < 8))
            oledBuffer[(bmp_y + 1) * 128 + bmp_x + i] = data_dis[i + 32 * 1];
        if ((bmp_x + i < 128) && (bmp_y + 2 < 8))
            oledBuffer[(bmp_y + 2) * 128 + bmp_x + i] = data_dis[i + 32 * 2];
        if ((bmp_x + i < 128) && (bmp_y + 3 < 8))
            oledBuffer[(bmp_y + 3) * 128 + bmp_x + i] = data_dis[i + 32 * 3];

    }
}

void oled_show_deal(void)
{
    #if 0
    static uint8_t i = 0;
    uint8_t num = 0;
    char str_b[10];
    // oled_data_clear(); //    清屏
    // oled_show_char_ASCII(0, 0, 'a'+i);            // 显示字符函数乱码
    // oled_show_char_ASCII(0, 0, 'a'+i, 12);            // 显示字符函数正常
    // oled_show_char_ASCII(0, 16, 'a'+i, 16);            // 显示字符函数正常
    // oled_show_char_ASCII(0, 32, 'a'+i, 24);            // 显示字符函数正常
    // oled_show_char_ASCII(16, 0, 'a'+i, 32);            // 显示字符函数正常

    // dis_show_string(0, 0, 128, 64, "hello world", 12);
    dis_show_string(0, 16, 128, 64, "hello world", 16);
    // lcd_show_string(0, 32, 128, 64, "hello world", 24);
    num = intToStr(123456, str_b);
    // dis_show_num(0, 48, 123456, 12);
    dis_show_num(0, 32, num, 12);
    dis_show_num_dis_0(0, 48, 9, 123456, 12);

    oled_draw_point(125, 61, 1);
    oled_draw_point(126, 62, 1);
    oled_draw_point(127, 63, 1);

    if (i < 26)
    {
        i++;
    }
    else
    {
        i = 0;
    }
    #endif
    oled_refresh_gram();
    oled_data_clear(); //    清屏
}
